define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/highsea/index',
                    // add_url: 'business/highsea/add',
                    apply_url: 'business/highsea/apply',
                    allot_url:'business/highsea/allot',
                    del_url: 'business/highsea/del',
                    // multi_url: 'business/highsea/multi',
                    table: 'business',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        {
                            field: 'gender',
                            title: __('Gender'),
                            searchList: { '0': __('Gender 0'), '1': __('Gender 1'), '2': __('Gender 2') },
                            operate: 'FIND_IN_SET',
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'deal',
                            title: __('Deal'),
                            searchList: { '0': __('Deal 0'), '1': __('Deal 1') },
                            operate: 'FIND_IN_SET',
                            formatter: Table.api.formatter.normal
                        },
                        { field: 'source.name', title: __('SourceName'), operate: 'LIKE' },
                        { field: 'admin.nickname', title: __('AdminName'), operate: 'LIKE' },
                        { field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'apply',
                                    text: '申请',
                                    title: function (row) {
                                        return `申请${row.nickname}客户`
                                    },
                                    extend: 'data-toggle="tooltip"',
                                    icon: 'fa fa-pencil',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.apply_url,
                                    confirm: '是否确认申请',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (err) {
                                        console.log(err);
                                    }
                                },
                                {
                                    name: 'allot', text: '分配',
                                    title:  function (row) {
                                        return `分配${row.nickname}客户`
                                    },
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-success btn-xs btn-dialog',
                                    icon: 'fa fa-pencil',
                                    url: 'business/highsea/allot?ids={id}',
                                }
                            ]

                        }
                    ]
                ],
                escape: false,
                // search:false,
            });

            $('.apply').on('click', function () {
                let ids = Table.api.selectedids(table);
                ids = ids.toString()
                layer.confirm('确定要申请吗?', { title: '申请', btn: ['是', '否'] },
                    function (index) {
                        layer.close(index);
                        $.post("business/highsea/apply", { ids: ids, action: 'success', reply: '' }, function (response) {
                            if (response.code == 1) {
                                Toastr.success(response.msg)
                                $(".btn-refresh").trigger('click');
                            } else {
                                Toastr.error(response.msg)
                            }
                        }, 'json')
                    },
                    function (index) {
                        layer.close(index);
                    }
                );
            });
            // 分配
            $('.allot').on('click', function () {
                let ids = Table.api.selectedids(table);
                ids = ids.toString()
                Fast.api.open($.fn.bootstrapTable.defaults.extend.allot_url + "?ids=" + ids, '分配')
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        apply: function () {
            Controller.api.bindevent();
        },
        allot: function () {
            Controller.api.bindevent();
        },
        del: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});